package com.sanno.patient.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.sanno.common.entity.*;
import com.sanno.common.entity.vo.SccMedicalResultVoPatient;
import com.sanno.common.result.Result;
import com.sanno.patient.config.CacheManagerConfig;
import com.sanno.patient.dao.*;
import com.sanno.patient.service.impl.PatientServiceImpl;

import com.sanno.patient.util.RedisUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patientController")
@CrossOrigin
public class PatientController {
    @Resource
    private PatientServiceImpl patientService;
    @Resource
    private MedicalResultMapper medicalResultMapper;

    @Resource
    private ReportList reportList;

    @Resource
    private PatientMapper patientMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MedicalReport medicalReport;

    private String patientKey="";

    @RequestMapping(value = "list" ,method = RequestMethod.GET)
    public Result getlist(String searchParams,int page,int limit) throws JsonProcessingException {

        String key=PatientController.class.getPackage().getName()+Thread.currentThread() .getStackTrace()[1].getMethodName();
        this.patientKey=key;

        if (searchParams!=null){
            redisUtil.delete(patientKey);
            redisUtil.delete(patientKey+"page");
        }

        if (redisUtil.get(key)!=null){
            return Result.ok(redisUtil.get(key),Long.parseLong((String) redisUtil.get(key+"page")));
        }else {
            Page<SccPatient> page1=new Page<>(page,limit);
            SccPatient sccPatient=new SccPatient();
            System.out.println("第一次");
            if (searchParams==null) {

                IPage<SccPatient> listPage= this.patientService.list(null,page1);
                redisUtil.set(key,listPage.getRecords());
                redisUtil.set(key+"page", String.valueOf(listPage.getTotal()));
                return Result.ok(listPage.getRecords(),listPage.getTotal());
            }
            redisUtil.delete(patientKey);
            ObjectMapper objectMapper = new ObjectMapper();
            sccPatient = objectMapper.readValue(searchParams, SccPatient.class);
            IPage<SccPatient> listPage= this.patientService.list(sccPatient,page1);
            return Result.ok(listPage.getRecords(),listPage.getTotal());
        }

    }

    @RequestMapping(value = "add" ,method = RequestMethod.POST)
    public Result addlist(SccPatient sccPatient){
        Integer add = patientService.add(sccPatient);
        redisUtil.delete(patientKey);
        redisUtil.delete(patientKey+"page");
        return Result.ok(add);
    }
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Result deletelist(String patientId){
        System.out.println(patientId);
        Integer delete = patientService.delete(patientId);
        return Result.ok(delete);
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public Result updatelist(SccPatient sccPatient){
        Integer update = patientService.update(sccPatient);
        return Result.ok(update);
    }
    @RequestMapping(value = "addResult",method = RequestMethod.POST)
    public Result addResult(SccMedicalResultVoPatient sccMedicalResult){
        System.out.println(sccMedicalResult);
        Integer integer = patientService.addResult(sccMedicalResult);
        MPJQueryWrapper<SccPatient> mpjQueryWrapper=new MPJQueryWrapper();
        mpjQueryWrapper.eq("Patient_id",sccMedicalResult.getPatientId());
        //修改身高体重
        SccPatient sccPatient=new SccPatient();
        sccPatient.setWeight(sccMedicalResult.getWeight());
        sccPatient.setHeight(sccMedicalResult.getHeight());
        patientMapper.update(sccPatient,mpjQueryWrapper);

        //添加报表数据
        Double[] arr=new Double[]{sccMedicalResult.getGluResult(),sccMedicalResult.getHbaicResult(),sccMedicalResult.getKetResult(),
        sccMedicalResult.getUaResult(),sccMedicalResult.getCholResult(),sccMedicalResult.getTgResult(),sccMedicalResult.getHdiCResult(),
        sccMedicalResult.getLdlCResult(),sccMedicalResult.getTcHdiCResult(),sccMedicalResult.getSbpResult(),sccMedicalResult.getDbpResult()
        };

        List<SccMedicalReport> list = new ArrayList<>();
        int i=1;
        for (Double aDouble : arr) {
            System.out.println(aDouble);
            MPJQueryWrapper<SccMedicalReport> mpjQueryWrapper1=new MPJQueryWrapper();
            mpjQueryWrapper1.selectAll(SccMedicalReport.class);
            mpjQueryWrapper1.eq("t.Medical_code",i);
            mpjQueryWrapper1.lt("t.Reference_low",aDouble);
            mpjQueryWrapper1.gt("t.Reference_high",aDouble);
            list.add(medicalReport.selectJoinOne(SccMedicalReport.class,mpjQueryWrapper1));
            i++;
        }
        int a=0;
        System.out.println(list);
        for (SccMedicalReport sccMedicalReport : list) {
            SccReportList sccReportList=new SccReportList();
            sccReportList.setMedicalResult(String.valueOf(arr[a]));
            sccReportList.setResultId(sccMedicalResult.getResultId());
            sccReportList.setMedicalCode(sccMedicalReport.getMedicalCode());
            sccReportList.setMedicalName(sccMedicalReport.getMedicalName());
            sccReportList.setMedicalUnits(sccMedicalReport.getMedicalUntis());
            sccReportList.setMedicalReference(sccMedicalReport.getMedicalReference());
            sccReportList.setMedicalMethod(sccMedicalReport.getMedicalMethod());
            sccReportList.setMedicalStatus(sccMedicalReport.getMedicalStatus());
            sccReportList.setMedicalSuggest(sccMedicalReport.getMedicalSuggest());
            reportList.insert(sccReportList);
            a++;
        }
        System.out.println(list);
        return Result.ok(integer);
    }
    @RequestMapping(value = "selectResult",method = RequestMethod.GET)
    public Result selectResult(String Pid,int page,int limit){
        System.out.println(Pid);
        IPage<SccMedicalResult> sccMedicalResultIPage = medicalResultMapper.selectJoinPage(new Page<>(page, limit), SccMedicalResult.class,
                new MPJQueryWrapper<>()
                        .select("t.Patient_id", "t.Create_time","t.Result_id")
                        .select("Store.Store_name")
                        .leftJoin("scc_drugstore Store on t.drugstore_id=Store.Store_id")
                        .eq("t.Patient_id", Pid)
        );
        return Result.ok(sccMedicalResultIPage.getRecords(),sccMedicalResultIPage.getTotal());
    }
    @RequestMapping(value = "selectResult1",method = RequestMethod.GET)
    public Result selectResult1(String Rid,int page,int limit){
        System.out.println(Rid);
        Page<SccReportList> result_id = reportList.selectPage(new Page<>(page, limit),
                new QueryWrapper<SccReportList>()
                        .eq("Result_id", Rid)
        );
        System.out.println(result_id.getRecords());
        return Result.ok(result_id.getRecords(),result_id.getTotal());
    }

}
